package ru.ifmo.croak.impl.storage.team1;

import com.sun.istack.internal.Nullable;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

/**
 * @author Alex Vikharev
 *         24.05.11
 */
public class Pattern {

    private static final String DELIMETERS = " \t\n\r\f+\"*%&/()=?'!,.;:-_#@|^~`{}[]";
    private final Set<String> patternWords;

    public Pattern(@Nullable final String pattern) {
        this.patternWords = pattern == null ? null : split(pattern);
    }

    public boolean matches(String text) {
        return patternWords == null || split(text).containsAll(patternWords);
    }

    public boolean matches(Collection<String> words){
        return patternWords == null || words.containsAll(patternWords);
    }

    public static Set<String> split(final String text) {
        StringTokenizer tokenizer =  new StringTokenizer(text, DELIMETERS);

        Set<String> set = new HashSet<String>();
        while (tokenizer.hasMoreTokens()) {
            String word = tokenizer.nextToken();
            word = word.toUpperCase();
            set.add(word);
        }
        return set;
    }
}
